﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using hStatsC.Models;
using Newtonsoft.Json;

namespace hStatsC.Helper
{
    public class MainHelper
    {
        private const string server = "http://api.heerdev.top:4999";
        public static bool Reg(string u, string pwd)
        {
            var r = HttpHelper.Get($"{server}/user/reg?user={u}&pwd={pwd}&token={"82HS9J9SDGI8"}");
            if (r != "true")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static void GetAppInfos()
        {
            var url = $"{server}/user/getapps?user={App.User.Name}&pwd={App.User.Password}";
            var r = HttpHelper.Get(url);

            if (r == "")
            {
                App.Apps = new ObservableCollection<AppInfo>();
                return;
            }
            else
            {
                App.Apps =new ObservableCollection<AppInfo>(JsonConvert.DeserializeObject<List<AppInfo>>(r)??new List<AppInfo>());
                return;
                
            }
        }

        public static bool AddApp(string appid)
        {
            
            var url = $"{server}/app/add?user={App.User.Name}&pwd={App.User.Password}&appid={appid}";
            var r = HttpHelper.Get(url);
            if (r == "true")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool RemoveApp(string appid)
        {
            
            var url = $"{server}/app/remove?user={App.User.Name}&pwd={App.User.Password}&appid={appid}";
            var r = HttpHelper.Get(url);
            if (r == "true")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static int Today(string appid)
        {
            
            var url = $"{server}/AppLaunchTimes/Today?appid={appid}";
            var r = HttpHelper.Get(url);
            if (r == "")
            {
                return 0;
            }
            else
            {
                
                return int.Parse(r);
            }
        }
        public static int NowOnline(string appid)
        {
            
            var url = $"{server}/Online/Count?appid={appid}";
            var r = HttpHelper.Get(url);
            if (r == "")
            {
                return 0;
            }
            else
            {
                
                return int.Parse(r);
            }
        }
        public static int Total(string appid)
        {
            return AllLaunchTimes(appid).Sum(x=>x.Times);
        }
        public static List<AppLaunchTime> AllLaunchTimes(string appid)
        {
            
            var url = $"{server}/AppLaunchTimes/all?appid={appid}";
            var r = HttpHelper.Get(url);
            if (r == "")
            {
                return new List<AppLaunchTime>();
            }
            else
            {
               return JsonConvert.DeserializeObject<List<AppLaunchTime>>(r) ?? new();
                
            }
        }

        public static List<AppVersion> AllVersions(string appid)
        {
            var url = $"{server}/AppVersions/All?appid={appid}";
            var r = HttpHelper.Get(url);
            if (r == "")
            {
                throw new Exception("AllAppVersions获取失败");
            }
            else
            {
                return JsonConvert.DeserializeObject<List<AppVersion>>(r) ?? new();
            }
        }
        
        public static bool AddVersion(string appid,AppVersion version)
        {
            var app = App.Apps.ToList().Find(x => x.App == appid);
            if (app == null)
            {
                return false;
            }
            var url = $"{server}/AppVersions/add?token={app.AccessToken}";
            var json = JsonConvert.SerializeObject(version);
            var r = HttpHelper.Post(url,json);
            if (r == "")
            {
                throw new Exception($"添加App版本失败!\n{json}");
            }
            else
            {
                return bool.Parse(r);
            }
        }
        public static bool DeleteVersion(string appid,AppVersion version)
        {
            var app = App.Apps.ToList().Find(x => x.App == appid);
            if (app == null)
            {
                return false;
            }
            var url = $"{server}/AppVersions/delete?appid={appid}&appversion={version.Version}&token={app.AccessToken}";
            var r = HttpHelper.Get(url);
            if (r == "true")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool Login(string u, string pwd)
        {
            var r = HttpHelper.Get($"{server}/user/login?user={u}&pwd={pwd}");
            if (r!= "")
            {
                //用户存在
                App.User = JsonConvert.DeserializeObject<UserInfo>(r);
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}